﻿/*----------------------------------------------------------------
// Copyright (C) 2014 苏州麦迪斯顿医疗科技股份有限公司
// 文件名：NetworkAdapterConnection.cs
// 文件功能描述：NetworkAdapterConnection
// 创建标识：顾伟伟-2014-04-03
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace CPOE.Framework
{
    /// <summary>
    /// 一个表示本地连接的类，用于测试网卡是否连接网络
    /// </summary>
    public class NetworkAdapterConnection : Connection
    {
         /// <summary>
        /// 更新连接状态
        /// </summary>
        public override void UpdateStatus(int timeout)
        {
            NetworkInterface adapter = null;
            //寻找可用的本地网络接口适配器
            foreach (NetworkInterface theAdapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (theAdapter.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
                    theAdapter.OperationalStatus == OperationalStatus.Up)
                {
                    adapter = theAdapter;
                    break;
                }
            }
            this.Name = adapter != null ? adapter.Name : "无网络连接";
            this.IP = adapter != null ? adapter.GetPhysicalAddress().ToString() : string.Empty;
            this.IsConnected =NetworkInterface.GetIsNetworkAvailable() && adapter != null;
            base.UpdateStatus(timeout);
        }
        
    }
}
